Expand description
console is a library for Rust that provides access to various terminal features so you can build nicer looking command line interfaces. It comes with various tools and utilities for working with Terminals and formatting text.
Best paired with other libraries in the family:
Terminal Access
The terminal is abstracted through the console::Term
type. It can
either directly provide access to the connected terminal or by buffering
up commands. A buffered terminal will however not be completely buffered
on windows where cursor movements are currently directly passed through.
Example usage:
use std::thread;
use std::time::Duration;
use console::Term;
let term = Term::stdout();
term.write_line("Hello World!")?;
thread::sleep(Duration::from_millis(2000));
term.clear_line()?;
Colors and Styles
console
automaticaly detects when to use colors based on the tty flag. It also
provides higher level wrappers for styling text and other things that can be
displayed with the style
function and utility types.
Example usage:
use console::style;
println!("This is {} neat", style("quite").cyan());
You can also store styles and apply them to text later:
use console::Style;
let cyan = Style::new().cyan();
println!("This is {} neat", cyan.apply_to("quite"));
Working with ANSI Codes
The crate provids the function strip_ansi_codes
to remove ANSI codes
from a string as well as measure_text_width
to calculate the width of a
string as it would be displayed by the terminal. Both of those together
are useful for more complex formatting.
Unicode Width Support
By default this crate depends on the unicode-width
crate to calculate
the width of terminal characters. If you do not need this you can disable
the unicode-width
feature which will cut down on dependencies.
Features
By default all features are enabled. The following features exist:
unicode-width
: adds support for unicode width calculationsansi-parsing
: adds support for parsing ansi codes (this adds support for stripping and taking ansi escape codes into account for length calculations).
Structs
- An iterator over ansi codes in a string.
- “Intelligent” emoji formatter.
- A stored style that can be applied.
- A formatting wrapper that can be styled for a terminal.
- Abstraction around a terminal.
- Gives access to the terminal features.
Enums
- Defines the alignment for padding operations.
- A terminal style attribute.
- A terminal color.
- Key mapping
- The family of the terminal.
- Where the term is writing.
Functions
- Returns
true
if colors should be enabled for stdout. - Returns
true
if colors should be enabled for stderr. - Measure the width of a string in terminal characters.
- Pads a string to fill a certain number of characters.
- Pads a string with specific padding to fill a certain number of characters.
- Forces colorization on or off for stdout.
- Forces colorization on or off for stderr.
- Helper function to strip ansi codes.
- Wraps an object for formatting for styling.
- Truncates a string to a certain number of characters.
- A fast way to check if the application has a user attended for stdout.
- A fast way to check if the application has a user attended for stderr.